package com.betterjr.modules.customer.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.betterjr.common.utils.BTAssert;
import com.betterjr.common.utils.BetterDateUtils;
import com.betterjr.common.utils.Collections3;
import com.betterjr.common.utils.QueryTermBuilder;
import com.betterjr.common.utils.UserUtils;
import com.betterjr.modules.account.service.CustAccountService;
import com.betterjr.modules.customer.AbstractBaseMechCustTemDataHandlerAdapter;
import com.betterjr.modules.customer.dao.CustMechShareholderMapper;
import com.betterjr.modules.customer.entity.CustMechShareholder;
import com.betterjr.modules.customer.entity.CustMechShareholderTmp;
import com.betterjr.modules.document.service.CustFileItemService;
import com.betterjr.modules.temhandler.data.TemDataConstant;
import com.betterjr.modules.temhandler.entity.TemData;
import com.betterjr.modules.temhandler.entity.TemDataBuilder;
import com.betterjr.modules.temhandler.service.TemDataService;

/**
 * 股东信息管理
 * @author liuwlBaseService<CustMechShareholderMapper, CustMechShareholder>
 *
 */
@Service
public class CustMechShareholderService
        extends AbstractBaseMechCustTemDataHandlerAdapter<CustMechShareholderMapper, CustMechShareholder> {

    @Resource
    private CustMechShareholderTmpService shareholderTmpService;

    @Resource
    private CustFileItemService fileItemService;

    @Resource
    private CustAccountService accountService;

    @Autowired
    private TemDataService temDataService;

    /**
     * 查询股东列表
     * @param anCustNo
     * @return
     */
    public List<CustMechShareholder> queryShareholder(Long anCustNo) {
        BTAssert.notNull(anCustNo, "客户编号不允许为空！");

        return this.selectByProperty("custNo", anCustNo);
    }

    /**
     * 查询股东信息
     */
    public CustMechShareholder findShareholder(Long anId) {
        BTAssert.notNull(anId, "股东编号不允许为空！");

        CustMechShareholder Shareholder = this.selectByPrimaryKey(anId);
        return Shareholder;
    }

    /**
     * 添加股东信息
     */
    public CustMechShareholder addCustMechShareholder(CustMechShareholderTmp anShareholderTmp) {
        BTAssert.notNull(anShareholderTmp, "股东流水信息不允许为空！");

        CustMechShareholder shareholder = new CustMechShareholder();
        shareholder.initAddValue(anShareholderTmp);

        this.insert(shareholder);
        return shareholder;
    }

    /**
     * 保存股东信息
     */
    public CustMechShareholder saveCustMechShareholder(CustMechShareholder anCustMechShareholder, Long anId) {
        BTAssert.notNull(anId, "股东编号不允许为空！");
        BTAssert.notNull(anCustMechShareholder, "股东信息不允许为空！");

        final CustMechShareholder tempCustMechShareholder = this.selectByPrimaryKey(anId);
        BTAssert.notNull(tempCustMechShareholder, "对应的股东信息没有找到！");

        tempCustMechShareholder.initModifyValue(anCustMechShareholder);
        this.updateByPrimaryKeySelective(tempCustMechShareholder);
        return tempCustMechShareholder;
    }

    /**
     * 通过股东流水修改股东 
     */
    public CustMechShareholder saveCustMechShareholder(CustMechShareholderTmp anShareholderTmp) {
        BTAssert.notNull(anShareholderTmp, "股东流水编号不允许为空！");

        final CustMechShareholder tempCustMechShareholder = this.selectByPrimaryKey(anShareholderTmp.getRefId());
        BTAssert.notNull(tempCustMechShareholder, "对应的股东信息没有找到！");

        tempCustMechShareholder.initModifyValue(anShareholderTmp);
        this.updateByPrimaryKeySelective(tempCustMechShareholder);
        return tempCustMechShareholder;
    }

    @SuppressWarnings("unchecked")
    @Override
    public TemData saveAddOrUpdateTemData(TemData anTemData) {
        // 插入校验
        Map<String, Object> map = QueryTermBuilder.newInstance().put("custNo", anTemData.getCustNo()).build();
        List<TemData> temDataList = temDataService.queryTemData(anTemData.getBusinTableType(), "1,2,3", map, "2", 1,
                10);
        BTAssert.isTrue(Collections3.isEmpty(temDataList), "已经有变更在申请过程中");
        return temDataService.saveAddTemData(anTemData);
    }

    public CustMechShareholder saveAddCustMechShareholderTem(CustMechShareholder custMechShareholder,
            String anFileList) {
        BTAssert.notNull(custMechShareholder, "公司基本信息-流水信息 不能为空！");
        BTAssert.notNull(custMechShareholder.getCustNo(), "公司基本信息-流水信息 不能为空！");
        custMechShareholder.saveAddInitValue(UserUtils.getOperatorInfo());
        custMechShareholder
                .setBatchNo(fileItemService.updateCustFileItemInfo(anFileList, custMechShareholder.getBatchNo()));
        TemData anTemData = convertToTemData(custMechShareholder);
        saveAddOrUpdateTemData(anTemData);
        return custMechShareholder;
    }

    private TemData convertToTemData(CustMechShareholder custMechShareholder) {

        TemData anTemData = TemDataBuilder.newInstance().businTableData(JSONObject.toJSONString(custMechShareholder))
                .businTableId(custMechShareholder.getId() + "").businTableOperorg(custMechShareholder.getOperOrg())
                .businTableType(custMechShareholder.getClass().getName()).laster(TemDataConstant.TEMDATA_LASTER_YES)
                .custNo(custMechShareholder.getCustNo())
                .custName(accountService.selectByPrimaryKey(custMechShareholder.getCustNo()).getCustName())
                .regDate(BetterDateUtils.getNumDate()).regOperid(UserUtils.getOperatorInfo().getId())
                .regOpername(UserUtils.getOperatorInfo().getName()).regTime(BetterDateUtils.getNumTime())
                .businStatus(TemDataConstant.TEMDATA_BUSINSTATUS_TEMTABLEINSERT).build();

        return anTemData;
    }

}
